<?php

namespace app\index\controller;

use think\Db;
use app\index\controller\Main;

// 商家移动端
class Merch extends Main {
    


    public function index() {

        $_GPC = input();
        $title = '活动管理';
        $_GPC['page']=isset($_GPC['page'])?$_GPC['page']:1;
        $_GPC['limit']=isset($_GPC['limit'])?$_GPC['limit']:10;
        $page = max(intval($_GPC['page']), 1);
        $limit = max(intval($_GPC['limit']), 10);

        $row = Db::name('sale')->where('member_id', $this->merchinfo['id']);
        if (!empty($_GPC['title'])) {
            $row = $row->whereLike('title', "%{$_GPC['title']}%");
        }else{
            $_GPC['title']="";
        }
        if (isset($_GPC['paytype']) && $_GPC['paytype'] != '') {
            $row = $row->where('paytype', $_GPC['paytype']);
        }else{
            $_GPC['paytype']="";
        }
        isset($_GPC['status']) or $_GPC['status'] = '1';
        $row = $_GPC['status'] == '1' ? $row->where('endtime', '>', time() - 86400) : $row->where('endtime', '<', time() - 86400);
        $list = $row->page($page, $limit)->order('endtime', 'DESC')->select();
        $count = $row->count();
       
        $this->view->assign('page', $page);
        $this->view->assign('limit', $limit);
        
        $this->view->assign('_GPC', $_GPC);
        $this->view->assign('count', $count);
        $this->view->assign('list', $list);
        return $this->view->fetch();
    }

    public function createsale() {

        $_GPC = input();
        $title = '创建活动';
        if ($this->request->isPost()) {
            $member = pdo_get('member', array('id' => $this->merchinfo['id']));
            if ($member['num'] != 0 && max($member['num'], 0) < $_GPC['data']['number']) {
                json('', 1, '商家点数剩余：' . max($member['num'], 0) . '，扫码次数最多只能填写：' . max($member['num'], 0) . '！');
            }
            if ($member['num'] != 0 && $_GPC['data']['number'] == 0) {
                json('', 1, '扫码次数必须大于0！');
            }
            if ($member['sale_num'] != 0) {

                $sale_num = Db::name('sale')->where(array('member_id' => $this->merchinfo['id']))->count();
                if ($member['sale_num'] <= $sale_num) {
                    json('', 1, '商家活动数量已满：' . $member['sale_num'] . '，不能添加新活动！');
                }
            }
            $_GPC['data']['member_id'] = $this->merchinfo['id'];
            $_GPC['data']['createtime'] = time();
            $_GPC['data']['starttime'] = strtotime($_GPC['data']['starttime']);
            $_GPC['data']['endtime'] = strtotime($_GPC['data']['endtime']);
            $_GPC['data']['poster'] = htmlspecialchars_decode($_GPC['data']['poster']);
            $_GPC['data']['content'] = htmlspecialchars_decode($_GPC['data']['content']);
            $_GPC['data']['video'] = json_encode($_GPC['data']['video']);
            $_GPC['data']['replace'] = json_encode($_GPC['data']['replace']);
            if ($_GPC['data']['card']['list']) {
                $_GPC['data']['card']['name'] = $_GPC['data']['card']['list'][0]['name'];
                $_GPC['data']['card']['term'] = $_GPC['data']['card']['list'][0]['term'];
                $_GPC['data']['card']['num'] = $_GPC['data']['card']['list'][0]['num'];
                $_GPC['data']['card']['limit'] = $_GPC['data']['card']['list'][0]['limit'];
                $_GPC['data']['card']['desc'] = $_GPC['data']['card']['list'][0]['desc'];
                $_GPC['data']['card']['idx'] = 0;
            }
            $_GPC['data']['card'] = json_encode($_GPC['data']['card']);
            $result = pdo_insert('sale', $_GPC['data']);
            if (!empty($result) || $result === 0) {
                if ($member['num'] != 0) {
                    $member['num'] = $member['num'] + $data['number'] - $_GPC['data']['number'];
                    $member['num'] != 0 or $member['num'] = -1;
                    pdo_update('member', $member, array('id' => $member['id']));
                }
                json('');
            } else {
                json('', 1, '提交失败！');
            }
        }
        
        $data=[
                'jump_url'=>"",
                'micro_app_id'=>"",
                'micro_app_url'=>"",
                'micro_app_title'=>"",
            ];
        
        $this->view->assign('data', $data);
        
        
        $this->view->assign('title', $title);
        return $this->view->fetch();
    }

    public function editsale() {

        $_GPC = input();
        $title = '编辑活动';
        $id = $_GPC['id'];
        $data = pdo_get('sale', array('id' => $id));
        if ($this->request->isPost()) {
            $member = pdo_get('member', array('id' => $this->merchinfo['id']));
            if ($member['num'] != 0 && max($member['num'], 0) + $data['number'] < $_GPC['data']['number']) {
                json('', 1, '商家点数剩余：' . (max($member['num'], 0) + $data['number']) . '，扫码次数最多只能填写：' . (max($member['num'], 0) + $data['number']) . '！');
            }
            if ($member['num'] != 0 && $_GPC['data']['number'] == 0) {
                json('', 1, '扫码次数必须大于0！');
            }
            $_GPC['data']['starttime'] = strtotime($_GPC['data']['starttime']);
            $_GPC['data']['endtime'] = strtotime($_GPC['data']['endtime']);
            $_GPC['data']['poster'] = htmlspecialchars_decode($_GPC['data']['poster']);
            $_GPC['data']['content'] = htmlspecialchars_decode($_GPC['data']['content']);
            $_GPC['data']['video'] = json_encode($_GPC['data']['video']);
            $_GPC['data']['replace'] = json_encode($_GPC['data']['replace']);
            if ($_GPC['data']['card']['list']) {
                $_GPC['data']['card']['name'] = $_GPC['data']['card']['list'][0]['name'];
                $_GPC['data']['card']['term'] = $_GPC['data']['card']['list'][0]['term'];
                $_GPC['data']['card']['num'] = $_GPC['data']['card']['list'][0]['num'];
                $_GPC['data']['card']['limit'] = $_GPC['data']['card']['list'][0]['limit'];
                $_GPC['data']['card']['desc'] = $_GPC['data']['card']['list'][0]['desc'];
                $_GPC['data']['card']['idx'] = 0;
            }
            $_GPC['data']['card'] = json_encode($_GPC['data']['card']);
            $result = pdo_update('sale', $_GPC['data'], array('id' => $id));
            if (!empty($result) || $result === 0) {
                if ($member['num'] != 0) {
                    $member['num'] = $member['num'] + $data['number'] - $_GPC['data']['number'];
                    $member['num'] != 0 or $member['num'] = -1;
                    pdo_update('member', $member, array('id' => $member['id']));
                }
                json('');
            } else {
                json('', 1, '提交失败！');
            }
        }
        $data['replace'] = json_decode($data['replace'], true);
        $data['video'] = json_decode($data['video'], true);
        if (isset($data['video']['title']) || isset($data['video']['path'])) {
            $data['video']['list'][0] = array('title' => $data['video']['title'], 'path' => $data['video']['path']);
        }
        $data['card'] = json_decode($data['card'], true);
        if (!isset($data['card']['list'])) {
            $data['card']['idx'] = 0;
            $data['card']['list'][] = array('name' => $data['card']['name'], 'term' => $data['card']['term'], 'num' => $data['card']['num'], 'limit' => $data['card']['limit'], 'desc' => $data['card']['desc']);
        }
        $data['scope'] = explode(',', $data['scope']);
        $data['content'] = htmlspecialchars_decode($data['content']);
        if ($data['content'] == strip_tags($data['content'])) {
            $data['content'] = '<p>' . $data['content'] . '</p>';
        }
        $this->view->assign('_GPC', $_GPC);
        $this->view->assign('title', $title);
        $this->view->assign('data', $data);
        return $this->view->fetch();
    }

    public function createvideo() {

        $_GPC = input();
        $title = '生成视频';
        $id = $_GPC['id'];
        $data = pdo_get('sale', array('id' => $id));
        $video = json_decode($data['video'], true);
        if ($this->request->isPost()) {
            $video['alivideo'] = $_GPC['data'];
            $data['video'] = json_encode($video);
            $result = pdo_update('sale', $data, array('id' => $id));
            if (!empty($result) || $result === 0) {
                asyn_http_get($this->request->domain(true) . '/web/alivideo?sale_id=' . $id);
                json('');
            } else {
                json('', 1, '提交失败！');
            }
        }
        if(empty($video['alivideo']))
        {
            $video['alivideo']=[
                    'title'=>"",
                    'media'=>[],
                    'num'=>0
                ];
        }
        
        //print_r($video);exit;
        $this->view->assign('_GPC', $_GPC);
        $this->view->assign('data', $data);
        $this->view->assign('video', $video);
        $this->view->assign('title', $title);
        return $this->view->fetch();
    }

    public function recharge() {
        $_GPC = input();
        $title = '充值次数';
        $prices = explode("\n", $this->sysset['recharge']['prices']);
        if ($this->merchinfo['agent_level'] == 0) {
            $level = array('id' => 1,
                'province' => $this->merchinfo['province'],
                'city' => $this->merchinfo['city'],
                'district' => $this->merchinfo['area'],
                'level' => 0, 'name' => $this->merchinfo['isagent'] ? '普通代理' : '普通商户',
                'discount' => 100,
                'price' => 0,
                'num' => 0,
                'task' => '',
                'createtime' => 0);
        } else {
            $level = $this->sysset['agent_level'][$this->merchinfo['agent_level']];
        }

        $this->view->assign('title', $title);
        $this->view->assign('prices', $prices);
        return $this->view->fetch();
    }

    public function createlog() {
        $_GPC = input();
        if ($this->merchinfo['agent_id'] > 0) {
            $agent = pdo_get(ADDON_NAME . '_member', array('id' => $this->merchinfo['agent_id']));
        } else {
            $agent = '';
        }
        if ($this->merchinfo['agent_level'] == 0) {
            $level = array('id' => 1, 'province' => $this->merchinfo['province'], 'city' => $this->merchinfo['city'], 'district' => $this->merchinfo['area'], 'level' => 0, 'name' => $this->merchinfo['isagent'] ? '普通代理' : '普通商户', 'discount' => 100, 'price' => 0, 'num' => 0, 'task' => '', 'createtime' => 0);
        } else {
            $level = $this->sysset['agent_level'][$this->merchinfo['agent_level']];
        }
        if ($level['price'] > 0 && $level['num'] > 0) {
            $num = $level['num'];
        } elseif ($level['price'] > 0) {
            $num = intval($this->sysset['recharge']['basenum'] * $level['price'] / ($level['discount'] / 100));
        } else {
            $num = intval(floatval($this->sysset['recharge']['basenum'] / ($level['discount'] / 100)) * floatval($_GPC['price']));
        }
        $result = pdo_insert('recharge_log', array('member_id' => $this->merchinfo['id'], 'agent_id' => $this->merchinfo['agent_id'], 'agent' => json_encode($agent), 'province' => $this->merchinfo['province'], 'city' => $this->merchinfo['city'], 'district' => $this->merchinfo['area'], 'price' => $_GPC['price'], 'num' => $num, 'createtime' => $_W['timestamp']));
        if (!empty($result) || $result === 0) {
            json(Db::name('recharge_log')->getLastInsID());
        } else {
            json('0', 1, '提交失败！');
        }
    }

}
